package com.sky.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.exception.BaseException;
import com.sky.exception.ParamsIllegalException;
import com.sky.mapper.common.DishFlavorMapper;
import com.sky.mapper.common.DishMapper;
import com.sky.mapper.common.SetmealDishMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.common.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @Author:JinFeng
 * @Date: 2025/06/13 20:56
 * @Description:
 */


@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public Result<List<Dish>> findByCategoryId(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        List<Dish> dishList = dishMapper.selectByNameStatusCategoryId(dish);
        return Result.success(dishList);
    }



    @Override
    public Result<List<Dish>> findByCategoryByName(String name) {
        Dish dish = Dish.builder()
                .name(name)
                .status(StatusConstant.ENABLE)
                .build();
        List<Dish> dishList = dishMapper.selectByNameCategory(dish);
        return Result.success(dishList);
    }

    @Transactional
    @Override
    public Result<String> updateWithFlavors(DishDTO dishDTO) {
        //参数校验

        //二.业务处理
        //1.修改菜品基本数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);
        //2.修改菜品口味数据
        //2.1删除原有的口味数据
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        //2,2 增加关联数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(dishFlavor -> {
            dishFlavor.setDishId(dish.getId());
        });
        dishFlavorMapper.insertBatch(dishDTO.getFlavors());
        //三.封装数据
        return Result.success();
    }

    @Override
    public Result<DishVO> findByIdWithFlavors(Long id) {
        //一.校验数据
        //二.处理业务
        //1.查询菜品基本数据及口味数据
        DishVO dishVO = dishMapper.seletDishAndFlavorsById(id);
        //三.封装数据
        return Result.success(dishVO);
    }



    @Transactional
    @Override
    public Result<String> deleteByIds(List<Long> ids) {
        //1.参数校验
        //2.处理业务
        //1)启售中的菜品不能删除
        Long count = dishMapper.countByStatusAndIds(StatusConstant.ENABLE, ids);
        if (count > 0){
            throw new BaseException(MessageConstant.DISH_ON_SALE);
        }
        //2)被套餐关联的菜品不能删除
        Long count2 = setmealDishMapper.countByDishIds(ids);
        if (count2 > 0){
            throw new BaseException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //3)删除菜品后,关联的口味数据也需要删除
        dishMapper.deleteByIds(ids);
        dishFlavorMapper.deleteByDishIds(ids);
        //3.封装数据
        return Result.success();
    }



    @Override
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //1.参数校验
        //2.处理业务
        //1)分页
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        //2)查询
        Page<DishVO> page = dishMapper.selectDishsAndCategory(dishPageQueryDTO);
        //3.封装数据
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return Result.success(pageResult);
    }




    @Override
    @Transactional
    public Result<String> addWithFlavor(DishDTO dishDTO) {
        //1.参数校验
        if (Objects.isNull(dishDTO)
                || Objects.isNull(dishDTO.getCategoryId())
                || StringUtils.isBlank(dishDTO.getName())
                || Objects.isNull(dishDTO.getPrice())
                || StringUtils.isBlank(dishDTO.getImage())
        ) {
            throw new ParamsIllegalException("参数非法");
        }
        //2.处理业务
        //0)菜品名称去重校验
        Long count = dishMapper.countByName(dishDTO.getName());
        if (count > 0) {
            throw new BaseException("菜品名称已存在,请更换!!!");
        }
        // 1)保存菜品数据到菜品表
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        // 2)保存菜品口味数据到菜品口味表
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(dishFlavor -> {
            dishFlavor.setDishId(dish.getId());
        });
        dishFlavorMapper.insertBatch(flavors);
        //3.封装数据
        return Result.success();
    }
}
